<template>
	<view class="content">
		<u-navbar rightText="删除" :placeholder='true' title="編輯" leftIcon="arrow-leftward"
			@leftClick="leftClick('/pages/mine/setting/childcompany/index')" @rightClick="rightClick">
		</u-navbar>
		<view class="main">
			<view>
				<view class="form">
					<view class="form-l">
						所屬母公司
						<text style="color:#ED0000 ;">*</text>
					</view>
					<view class="form-r">
						<picker @change="bindPickerChange" :value="index" :range="array" range-key="branchname">
							<view class="picker">
								<view class="uni-input">{{branch.parentname || companyname}}</view>
								<u-icon size="28rpx" name="arrow-right"></u-icon>
							</view>
						</picker>
					</view>
				</view>
				<view class="form">
					<view class="form-l">
						分行/子公司編號
					</view>
					<view class="form-r">
						<input v-model="branch.branch_code" class="input-item" type="text">
					</view>
				</view>
				<view class="form">
					<view class="form-l">
						名稱（中）
						<text style="color:#ED0000 ;">*</text>
					</view>
					<view class="form-r">
						<input v-model="branch.branchname" class="input-item" type="text">
					</view>
				</view>
				<view class="form">
					<view class="form-l">
						名稱（英）
						<text style="color:#ED0000 ;">*</text>
					</view>
					<view class="form-r">
						<input v-model="branch.en_branchname" class="input-item" type="text" placeholder="">
					</view>
				</view>
				<view class="form">
					<view class="form-l">
						聯繫人
						<text style="color:#ED0000 ;">*</text>
					</view>
					<view class="form-r">
						<input v-model="branch.principal" class="input-item" type="text">
					</view>
				</view>
				<view class="form">
					<view class="form-l">
						電話
						<text style="color:#ED0000 ;">*</text>
					</view>
					<view class="form-r">
						<input v-model="branch.phone" class="input-item" type="text">
					</view>
				</view>
				<view class="form">
					<view class="form-l">
						郵箱
					</view>
					<view class="form-r">
						<input v-model="branch.email" class="input-item" type="text">
					</view>
				</view>
				<view class="form">
					<view class="form-l">
						地址
					</view>
					<view class="form-r">
						<input v-model="branch.address" class="input-item" type="text">
					</view>
				</view>
				<view class="form">
					<view class="form-l">
						傳真
					</view>
					<view class="form-r">
						<input v-model="branch.fax" class="input-item" type="text">
					</view>
				</view>
				<view class="form">
					<view class="form-l">
						分配金額
					</view>
					<view class="form-r">
						<input v-model="branch.amount" class="input-item" type="number">
					</view>
				</view>
				<view style="text-align: center;color: #909399;" v-if="branch.amount && branch.parentname">
					剩餘可分配額度：{{balance-branch.amount}}
				</view>
			</view>
		</view>
		<view class="btn" @click="submit">
			確定
		</view>
	</view>
</template>

<script>
	import {
		getparent,
		getcompanybyid,
		getamount,
		editconpany,
		delcompany
	} from '@/network/api/setting.js'
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				balance: '',
				branch_id: '',
				array: [],
				index: 0,
				value: false,
				branch: {
					parent_id: '',
					parentname: '',
					branch_id: '',
					branch_code: '',
					branchname: '',
					en_branchname: '',
					principal: '',
					phone: '',
					email: '',
					address: '',
					fax: '',
					amount: '',
				},
				companyname: ''

			}
		},
		computed: {
			...mapState({
				userinfo: state => state.user.userinfo,
			})
		},

		onLoad(e) {
			console.log(e.branch_id);
			this.branch_id = e.branch_id
			this.companyname = e.companyname
		},
		onShow() {
			this.getParent()
			this.getcompanydetails()
		},
		methods: {
			rightClick() {
				uni.showModal({
					title: '提示',
					content: '确认删除子公司/分行？',
					success: async res => {
						if (res.confirm) {
							let data = {
								branch_id: this.branch.branch_id
							}
							const res = await delcompany(data)
							if (res.data.code == 1) {
								uni.navigateBack({
									delta: 1
								})
							}
						} else if (res.cancel) {
							return
						}
					}
				});
			},
			async submit() {
				console.log(this.branch);
				if (!this.branch.branchname) {
					return uni.showToast({
						title: '所屬母公司不能为空',
						icon: 'none'
					})
				}
				if (!this.branch.branch_code) {
					return uni.showToast({
						title: '分行/子公司編號不能为空',
						icon: 'none'
					})
				}
				if (!this.branch.branchname) {
					return uni.showToast({
						title: '名稱（中）不能为空',
						icon: 'none'
					})
				}
				if (!this.branch.en_branchname) {
					return uni.showToast({
						title: '名稱（英）不能为空',
						icon: 'none'
					})
				}
				if (!this.branch.principal) {
					return uni.showToast({
						title: '聯繫人不能为空',
						icon: 'none'
					})
				}
				if (!this.branch.phone) {
					return uni.showToast({
						title: '電話不能为空',
						icon: 'none'
					})
				}
				const res = await editconpany(this.branch)
				if (res.data.code == 1) {
					uni.showToast({
						title: '修改成功',
					})
          setTimeout(() => {
            uni.navigateBack({
              delta: 1
            })
          }, 1000)
				}
			},
			async getParent() {
				let data = {
					company_id: this.userinfo.company_id
				}
				const res = await getparent(data)
				this.array = res.data.branch
				console.log(this.array);
			},
			async getcompanydetails() {
				let data = {
					branch_id: this.branch_id
				}
				const res = await getcompanybyid(data)
				this.branch = res.data.branch

			},
			async bindPickerChange(e) {
				console.log('picker发送选择改变，携带值为', e.detail.value)
				this.index = e.detail.value
				this.branch.parentname = this.array[this.index].branchname
				this.branch.parent_id = this.array[this.index].branch_id
				let data = {
					branch_id: this.array[this.index].branch_id
				}
				const res = await getamount(data)
				this.balance = res.data.balance
			},
			leftClick(url) {
				console.log('rightClick');
				uni.navigateTo({
					url: url
				})
			},
		}
	}
</script>

<style lang="scss">
	.content {
		background-color: #F5F6F9;
		width: 100%;
		height: 100vh;
		position: relative;

		.main {
			background-color: #fff;

			.form {
				margin: 0 32rpx;
				padding: 32rpx 0;
				display: flex;
				border-bottom: 1rpx solid #DEDEDE;
				;

				.form-l {
					font-size: 32rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #666666;
				}

				.form-r {
					margin-left: 48rpx;
					flex: 1;

					.picker {
						display: flex;
						align-items: center;
						justify-content: space-between;

						.uni-input {
							font-size: 32rpx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #333333;
						}
					}
				}
			}

			.form:last-child {
				border-bottom: none;
			}
		}

		.open {
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #666666;
			margin-top: 22rpx;
			background-color: #fff;
			padding: 32rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.btn {
			position: absolute;
			bottom: 15rpx;
			left: 50%;
			transform: translateX(-50%);
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: bold;
			line-height: 80rpx;
			color: #FFFFFF;
			width: 686rpx;
			height: 80rpx;
			text-align: center;
			background: #645AED;
			opacity: 1;
			border-radius: 30rpx;
		}
	}
</style>
